/*
 * Copyright (c) 2021 Talkweb Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "tw_nfc_kits.h"

static void nfc_read_tag(void)
{
    printf("========create nfc_read_tag!\n");
    TWNfcI2cInit();
    osDelay(10);

    NdefParsingList *head = TWNfcTagRead(); // 读取nfc eeprom中的标签值，返回值为读取到的标签列表头地址
    NdefParsingList *node = head->next;     
    while(head != NULL && node != NULL) // 打印所有读到的nfc标签
    {
        printf("nfcTag: ");
        for(int i=0;i< node->ndef.payloadLen;i++)
        {
            printf("%02X ", node->ndef.payload[i]);
        }
        printf("\r\n");
        node->ndef.payload[node->ndef.payloadLen] = 0;
        printf("nfcTag: %s\r\n",node->ndef.payload);
        node = node->next;
    }

    NfcReadTagFree(head); // 记得释放nfc读取时占用的资源
    printf("TWNfcTagRead end\r\n ");
    for (;;)
    {
        osDelay(100);
    }
}

static void TW_nfc_example(void)
{
    osThreadAttr_t attr;

    attr.name = "nfc_read_tag";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = 1024 * 8;
    attr.priority = 25;

    if (osThreadNew((osThreadFunc_t)nfc_read_tag, NULL, &attr) == NULL)
    {
        printf("Falied to create nfc_read_tag!\n");
    }
}

APP_FEATURE_INIT(TW_nfc_example);
